package com.zwt.mq;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.zwt.domain.TsPlatformUser;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * Created by zwt on 2018/9/13.
 * 消费者
 */
@Component
public class TestReceiver {

    /**
     *
     * @param tsPlatformUser
     * @param headers
     * @param channel
     * @throws IOException
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "test-queue",durable = "true"),
            exchange = @Exchange(name = "test-exchange",durable = "true",type = "topic"),
            key = "test.#"
                  )
    )
    @RabbitHandler
    public void onMessage(@Payload TsPlatformUser tsPlatformUser, @Headers Map<String,Object> headers, Channel channel) throws IOException {
        System.out.println("-----收到消息-----");
        System.out.println("nick是："+tsPlatformUser.getNick());
        Long tag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        channel.basicAck(tag,false);
    }
}
